﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Newtonsoft.Json.Linq;
using System.Collections.Specialized;

namespace MTA.TripPlanner
{
    public enum RouteDirection
    {
        North,
        South,
        East,
        West,
        Return,
        NoInfo
    }

    public static class RouteExtensions
    {
        public static RouteDirection GetRouteDirection(this String s)
        {
            switch (s[0])
            {
                case 'N':
                    return RouteDirection.North;
                case 'S':
                    return RouteDirection.South;
                case 'E':
                    return RouteDirection.East;
                case 'W':
                    return RouteDirection.West;
                case 'R':
                    return RouteDirection.Return;
                default:
                    return RouteDirection.NoInfo;
            }
        }

    }

    public class Route : BaseFeed<Route>
    {
        RouteDirection _direction = RouteDirection.NoInfo;
        String _name = String.Empty;

        public string Name { get { return _name; } }
        public RouteDirection Direction { get { return _direction; } }


        public static string FeedUrl
        {
            get { return ConfigurationManager.AppSettings["RoutePage"]; }
        }


        public static Route ConvertOne(JObject c)
        {
            return new Route() { _id = int.Parse((string)c["id"]), 
                    _name = (String)c["text"], 
                    _direction = ((String)c["direction"]).GetRouteDirection() };
        }

        public static List<Route> GetByCarrierId(int carrierid)
        {
            JSONRequest request = new JSONRequest(FeedUrl);
            request.Params["carrier"] = carrierid.ToString();

            return ConvertJSONArray(request, o => o["routes"]["item"], ConvertOne, true);

        }

    }
}
